package com.lettcode;

/**
 * @Description 两数相加
 * @Author Jlis
 * @Date 2021/7/18
 * @Version 1.0
 */
public class Lc0002 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        l1.next = new ListNode(9);
        l1.next.next = new ListNode(9);
        l1.next.next.next = new ListNode(9);
        l1.next.next.next.next = new ListNode(9);
        l1.next.next.next.next.next = new ListNode(9);
        l1.next.next.next.next.next.next = new ListNode(9);

        ListNode l2 = new ListNode(9);
        l2.next = new ListNode(9);
        l2.next.next = new ListNode(9);
        l2.next.next.next = new ListNode(9);
        addTwoNumbers(l1,l2);
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int jw = 0;
        ListNode l1Next = l1.next;
        ListNode l2Next = l2.next;
        int l1Num = l1.val;
        int l2Num = l2.val;
        int t = l1Num + l2Num + jw;
        if(t >= 10){
            jw = 1;
            t = t - 10;
        }
        ListNode retNode = new ListNode(t);
        ListNode curNode = retNode;
        while(true){
            l1Num = 0;
            l2Num = 0;
            boolean l1Null = false;
            boolean l2Null = false;
            if(l1Next != null){
                l1Num = l1Next.val;
                l1Next = l1Next.next;
            }else{
                l1Null = true;
            }
            if(l2Next != null){
                l2Num = l2Next.val;
                l2Next = l2Next.next;
            }else{
                l2Null = true;
            }
            if(l2Null && l1Null){
                if(jw == 1){
                    curNode.next = new ListNode(jw);
                }
                break;
            }
            t = l1Num + l2Num + jw;
            if(t >= 10){
                jw = 1;
                t = t - 10;
            }else{
                jw = 0;
            }
            curNode.next = new ListNode(t);
            curNode = curNode.next;
        }
        return retNode;
    }
}



/**
 * Definition for singly-linked list.
 **/
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

